package msqltest;

import java.sql.*;

public class Test300 {
    static final String USER = "root";
    static final String PASS = "root";
    static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";
    static final String DB_URL = "jdbc:mysql://localhost:3306/bank";
    public static void main(String[] args) {
        Connection conn = null;
        Statement stmt = null;
        try{
            // 注册 JDBC 驱动
            Class.forName(JDBC_DRIVER);

            // 打开链接
            System.out.println("连接数据库...");
            conn = DriverManager.getConnection(DB_URL,USER,PASS);

            // 执行查询
            System.out.println(" 实例化Statement对象...");
            stmt = conn.createStatement();
            String sql="INSERT INTO test_3000 VALUES";
            for (int i = 0; i <= 3000; i++) {
                if(i==3000){
                    String j = "z"+i;
                    String k = "w"+i;
                    sql = sql+"("+i+",'"+j+"','"+k+"')";
                    break;
                }
                String j = "z"+i;
                String k = "w"+i;
                sql = sql+"("+i+",'"+j+"','"+k+"')"+",";
            }
            stmt.executeUpdate(sql);
//            long startMs = System.currentTimeMillis();
//            String sql1 = "SELECT id, test, test_suo FROM test_3000 WHERE id= 2900";
//            ResultSet rs1 = stmt.executeQuery(sql1);
//            long endMs = System.currentTimeMillis();
//            // 展开结果集数据库
//            while(rs1.next()){
//                // 通过字段检索
//                int id  = rs1.getInt("id");
//                String test = rs1.getString("test");
//                String test_suo = rs1.getString("test_suo");
//                System.out.println(endMs-startMs);
//                System.out.print("ID: " + id);
//                System.out.print(", test: " + test);
//                System.out.print(", test_suo: " + test_suo);
//                System.out.println();
//            }
            // 完成后关闭
            stmt.close();
            conn.close();
        }catch(SQLException se){
            // 处理 JDBC 错误
            se.printStackTrace();
        }catch(Exception e){
            // 处理 Class.forName 错误
            e.printStackTrace();
        }finally{
            // 关闭资源
            try{
                if(stmt!=null) {
                    stmt.close();
                }
            }catch(SQLException se2){
            }// 什么都不做
            try{
                if(conn!=null) {
                    conn.close();
                }
            }catch(SQLException se){
                se.printStackTrace();
            }
        }
        System.out.println("Goodbye!");
    }
}
